home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue69 / OpenGL / DGLUT.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-12-29  |  28.8 KB  |  621 lines

  1. { DGLUT.PAS }{ Bob Crawford  F.L.A.S.K.  June, 1997 }{ DGLUT is an Object Pascal translation of a small part of Mark Kilgard's  GLUT library for OpenGL. Included here are just the shape-drawing routines  from glut_shapes.c and the teapot routines from glut_teapot.c. Following  is the original copyright notices from those files. }{ Copyright (c) Mark J. Kilgard, 1994.
  2. /**
  3. (c) Copyright 1993, Silicon Graphics, Inc.
  4.  
  5. ALL RIGHTS RESERVED
  6.  
  7. Permission to use, copy, modify, and distribute this software
  8. for any purpose and without fee is hereby granted, provided
  9. that the above copyright notice appear in all copies and that
  10. both the copyright notice and this permission notice appear in
  11. supporting documentation, and that the name of Silicon
  12. Graphics, Inc. not be used in advertising or publicity
  13. pertaining to distribution of the software without specific,
  14. written prior permission.
  15.  
  16. THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU
  17. "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR
  18. OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  19. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  IN NO
  20. EVENT SHALL SILICON GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE
  21. ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
  22. CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
  23. INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
  24. SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR
  25. NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF THE POSSIBILITY
  26. OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. US Government Users Restricted Rights
  31.  
  32. Use, duplication, or disclosure by the Government is subject to
  33. restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  34. (c)(1)(ii) of the Rights in Technical Data and Computer
  35. Software clause at DFARS 252.227-7013 and/or in similar or
  36. successor clauses in the FAR or the DOD or NASA FAR
  37. Supplement.  Unpublished-- rights reserved under the copyright
  38. laws of the United States.  Contractor/manufacturer is Silicon
  39. Graphics, Inc., 2011 N.  Shoreline Blvd., Mountain View, CA
  40. 94039-7311.
  41.  
  42. OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  43. }
  44. unit DGLUT;interface
  45. uses  OpenGL12;
  46. type  TGLfloat3v =    array[0..2] of TGLfloat;  TInteger3v =    array[0..2] of Integer;const  BoxPoints :    Array[0..5, 0..2] of TGLfloat =
  47.       ( (-1,  0,  0),
  48.         ( 0,  1,  0),
  49.         ( 1,  0,  0),
  50.         ( 0, -1,  0),
  51.         ( 0,  0,  1),
  52.         ( 0,  0, -1) );
  53.  
  54.   BoxFaces :    Array[0..5, 0..3] of TGLint =      ( (0, 1, 2, 3),
  55.         (3, 2, 6, 7),
  56.         (7, 6, 5, 4),
  57.         (4, 5, 1, 0),
  58.         (5, 6, 2, 1),
  59.         (7, 4, 0, 3) );
  60.  
  61.   { Octahedron data:      The octahedron produced is centered at the origin and has radius 1.0 }  OctData :    Array[0..5] of TGLfloat3v =      ( (1.0, 0.0, 0.0),
  62.         (-1.0, 0.0, 0.0),
  63.         (0.0, 1.0, 0.0),
  64.         (0.0, -1.0, 0.0),
  65.         (0.0, 0.0, 1.0),
  66.         (0.0, 0.0, -1.0) );
  67.  
  68.   OctIndex :
  69.     Array[0..7] of TInteger3v =
  70.       (  (0, 4, 2),
  71.          (1, 2, 4),
  72.          (0, 3, 4),
  73.          (1, 4, 3),
  74.          (0, 2, 5),
  75.          (1, 5, 2),
  76.          (0, 5, 3),
  77.          (1, 3, 5) );
  78.  
  79.   { Icosahedron data:      These numbers are rigged to make an icosahedron of radius 1.0 }
  80.   IcoX = 0.525731112119133606;
  81.   IcoZ = 0.850650808352039932;
  82.  
  83.   IcoData :
  84.     Array[0..11] of TGLfloat3v =
  85.       ( (-IcoX,     0,  IcoZ),
  86.         ( IcoX,     0,  IcoZ),
  87.         (-IcoX,     0, -IcoZ),
  88.         ( IcoX,     0, -IcoZ),
  89.         (    0,  IcoZ,  IcoX),
  90.         (    0,  IcoZ, -IcoX),
  91.         (    0, -IcoZ,  IcoX),
  92.         (    0, -IcoZ, -IcoX),
  93.         ( IcoZ,  IcoX,     0),
  94.         (-IcoZ,  IcoX,     0),
  95.         ( IcoZ, -IcoX,     0),
  96.         (-IcoZ, -IcoX,     0) );
  97.  
  98.   IcoIndex :
  99.     Array[0..19] of TInteger3v =
  100.       ( (0, 4, 1),
  101.         (0, 9, 4),
  102.         (9, 5, 4),
  103.         (4, 5, 8),
  104.         (4, 8, 1),
  105.         (8, 10, 1),
  106.         (8, 3, 10),
  107.         (5, 3, 8),
  108.         (5, 2, 3),
  109.         (2, 7, 3),
  110.         (7, 10, 3),
  111.         (7, 6, 10),
  112.         (7, 11, 6),
  113.         (11, 0, 6),
  114.         (0, 1, 6),
  115.         (6, 1, 10),
  116.         (9, 0, 11),
  117.         (9, 11, 2),
  118.         (9, 2, 5),
  119.         (7, 2, 11) );
  120.  
  121.   { Tetrahedron data }
  122.   TetT = 1.73205080756887729;
  123.  
  124.   TetData :
  125.     Array[0..3] of TGLfloat3v =
  126.       ( ( TetT,  TetT,  TetT),
  127.         ( TetT, -TetT, -TetT),
  128.         (-TetT,  TetT, -TetT),
  129.         (-TetT, -TetT,  TetT) );
  130.  
  131.   TetIndex :
  132.     Array[0..3] of TInteger3v =
  133.       (  (0, 1, 3),
  134.          (2, 1, 0),
  135.          (3, 2, 0),
  136.          (1, 2, 3)  );
  137.  
  138. { Teapot stuff }
  139.  
  140.   { Rim, body, lid, and bottom data must be reflected in x
  141.    and y; handle and spout data across the y axis only. }
  142.  
  143.   PatchData :
  144.     Array[0..9, 0..15] of TGLint =
  145.     (
  146.       { Rim }
  147.       (102, 103, 104, 105,   4,   5,   6,   7,
  148.          8,   9,  10,  11,  12,  13,  14,  15),
  149.       { Body }
  150.       ( 12,  13,  14,  15,  16,  17,  18,  19,
  151.         20,  21,  22,  23,  24,  25,  26,  27),
  152.       ( 24,  25,  26,  27,  29,  30,  31,  32,
  153.         33,  34,  35,  36,  37,  38,  39,  40),
  154.       { Lid *}
  155.       ( 96,  96,  96,  96,  97,  98,  99, 100,
  156.        101, 101, 101, 101,   0,   1,   2,   3),
  157.       (  0,   1,   2,   3, 106, 107, 108, 109,
  158.        110, 111, 112, 113, 114, 115, 116, 117),
  159.       { Bottom }
  160.       (118, 118, 118, 118, 124, 122, 119, 121,
  161.        123, 126, 125, 120,  40,  39,  38,  37),
  162.       { Handle }
  163.       ( 41,  42,  43,  44,  45,  46,  47,  48,
  164.         49,  50,  51,  52,  53,  54,  55,  56),
  165.       ( 53,  54,  55,  56,  57,  58,  59,  60,
  166.         61,  62,  63,  64,  28,  65,  66,  67),
  167.       { Spout }
  168.       ( 68,  69,  70,  71,  72,  73,  74,  75,
  169.         76,  77,  78,  79,  80,  81,  82,  83),
  170.       ( 80,  81,  82,  83,  84,  85,  86,  87,
  171.         88,  89,  90,  91,  92,  93,  94,  95) );
  172.  
  173.   TeaData :
  174.     Array[0..126, 0..2] of TGLfloat =
  175.     ( (   0.2,       0,     2.7),
  176.       (   0.2,  -0.112,     2.7),
  177.       ( 0.112,    -0.2,     2.7),
  178.       (     0,    -0.2,     2.7),
  179.       (1.3375,       0, 2.53125),
  180.       (1.3375,  -0.749, 2.53125),
  181.       ( 0.749, -1.3375, 2.53125),
  182.       (     0, -1.3375, 2.53125),
  183.       (1.4375,       0, 2.53125),
  184.       (1.4375,  -0.805, 2.53125),
  185.       ( 0.805, -1.4375, 2.53125),
  186.       (     0, -1.4375, 2.53125),
  187.       (   1.5,       0,     2.4),
  188.       (   1.5,   -0.84,     2.4),
  189.       (  0.84,    -1.5,     2.4),
  190.       (     0,    -1.5,     2.4),
  191.       (  1.75,       0,   1.875),
  192.       (  1.75,   -0.98,   1.875),
  193.       (  0.98,   -1.75,   1.875),
  194.       (     0,   -1.75,   1.875),
  195.       (     2,       0,    1.35),
  196.       (     2,   -1.12,    1.35),
  197.       (  1.12,      -2,    1.35),
  198.       (     0,      -2,    1.35),
  199.       (     2,       0,     0.9),
  200.       (     2,   -1.12,     0.9),
  201.       (  1.12,      -2,     0.9),
  202.       (     0,      -2,     0.9),
  203.       (    -2,       0,     0.9),
  204.       (     2,       0,    0.45),
  205.       (     2,   -1.12,    0.45),
  206.       (  1.12,      -2,    0.45),
  207.       (     0,      -2,    0.45),
  208.       (   1.5,       0,   0.225),
  209.       (   1.5,   -0.84,   0.225),
  210.       (  0.84,    -1.5,   0.225),
  211.       (     0,    -1.5,   0.225),
  212.       (   1.5,       0,    0.15),
  213.       (   1.5,   -0.84,    0.15),
  214.       (  0.84,    -1.5,    0.15),
  215.       (     0,    -1.5,    0.15),
  216.       (  -1.6,       0,   2.025),
  217.       (  -1.6,    -0.3,   2.025),
  218.       (  -1.5,    -0.3,    2.25),
  219.       (  -1.5,       0,    2.25),
  220.       (  -2.3,       0,   2.025),
  221.       (  -2.3,    -0.3,   2.025),
  222.       (  -2.5,    -0.3,    2.25),
  223.       (  -2.5,       0,    2.25),
  224.       (  -2.7,       0,   2.025),
  225.       (  -2.7,    -0.3,   2.025),
  226.       (    -3,    -0.3,    2.25),
  227.       (    -3,       0,    2.25),
  228.       (  -2.7,       0,     1.8),
  229.       (  -2.7,    -0.3,     1.8),
  230.       (    -3,    -0.3,     1.8),
  231.       (    -3,       0,     1.8),
  232.       (  -2.7,       0,   1.575),
  233.       (  -2.7,    -0.3,   1.575),
  234.       (    -3,    -0.3,    1.35),
  235.       (    -3,       0,    1.35),
  236.       (  -2.5,       0,   1.125),
  237.       (  -2.5,    -0.3,   1.125),
  238.       ( -2.65,    -0.3,  0.9375),
  239.       ( -2.65,       0,  0.9375),
  240.       (    -2,    -0.3,     0.9),
  241.       (  -1.9,    -0.3,     0.6),
  242.       (  -1.9,       0,     0.6),
  243.       (   1.7,       0,   1.425),
  244.       (   1.7,   -0.66,   1.425),
  245.       (   1.7,   -0.66,     0.6),
  246.       (   1.7,       0,     0.6),
  247.       (   2.6,       0,   1.425),
  248.       (   2.6,   -0.66,   1.425),
  249.       (   3.1,   -0.66,   0.825),
  250.       (   3.1,       0,   0.825),
  251.       (   2.3,       0,     2.1),
  252.       (   2.3,   -0.25,     2.1),
  253.       (   2.4,   -0.25,   2.025),
  254.       (   2.4,       0,   2.025),
  255.       (   2.7,       0,     2.4),
  256.       (   2.7,   -0.25,     2.4),
  257.       (   3.3,   -0.25,     2.4),
  258.       (   3.3,       0,     2.4),
  259.       (   2.8,       0,   2.475),
  260.       (   2.8,   -0.25,   2.475),
  261.       ( 3.525,   -0.25, 2.49375),
  262.       ( 3.525,       0, 2.49375),
  263.       (   2.9,       0,   2.475),
  264.       (   2.9,   -0.15,   2.475),
  265.       (  3.45,   -0.15,  2.5125),
  266.       (  3.45,       0,  2.5125),
  267.       (   2.8,       0,     2.4),
  268.       (   2.8,   -0.15,     2.4),
  269.       (   3.2,   -0.15,     2.4),
  270.       (   3.2,       0,     2.4),
  271.       (     0,       0,    3.15),
  272.       (   0.8,       0,    3.15),
  273.       (   0.8,   -0.45,    3.15),
  274.       (  0.45,    -0.8,    3.15),
  275.       (     0,    -0.8,    3.15),
  276.       (     0,       0,    2.85),
  277.       (   1.4,       0,     2.4),
  278.       (   1.4,  -0.784,     2.4),
  279.       ( 0.784,    -1.4,     2.4),
  280.       (     0,    -1.4,     2.4),
  281.       (   0.4,       0,    2.55),
  282.       (   0.4,  -0.224,    2.55),
  283.       ( 0.224,    -0.4,    2.55),
  284.       (     0,    -0.4,    2.55),
  285.       (   1.3,       0,    2.55),
  286.       (   1.3,  -0.728,    2.55),
  287.       ( 0.728,    -1.3,    2.55),
  288.       (     0,    -1.3,    2.55),
  289.       (   1.3,       0,     2.4),
  290.       (   1.3,  -0.728,     2.4),
  291.       ( 0.728,    -1.3,     2.4),
  292.       (     0,    -1.3,     2.4),
  293.       (     0,       0,       0),
  294.       ( 1.425,  -0.798,       0),
  295.       (   1.5,       0,   0.075),
  296.       ( 1.425,       0,       0),
  297.       ( 0.798,  -1.425,       0),
  298.       (     0,    -1.5,   0.075),
  299.       (     0,  -1.425,       0),
  300.       (   1.5,   -0.84,   0.075),
  301.       (  0.84,    -1.5,   0.075) );
  302.  
  303.   TeaTex :
  304.     Array[0..1, 0..1, 0..1] of TGLFloat =
  305.       ( ((0, 0), (1, 0)),
  306.         ((0, 1), (1, 1)) );
  307.  
  308.  
  309. procedure DrawBox(Size : TGLFloat; DrawType : TGLenum);procedure glutWireCube(Size : TglDouble);procedure glutSolidCube(Size : TglDouble);
  310. procedure glutWireSphere(
  311.   Radius : TglDouble;
  312.   Slices : TGLint;
  313.   Stacks : TGLint);
  314. procedure glutSolidSphere(
  315.   Radius : TglDouble;
  316.   Slices : TGLint;
  317.   Stacks : TGLint);
  318. procedure glutWireCone(
  319.   Base : TglDouble;
  320.   Height : TglDouble;
  321.   Slices : TGLint;
  322.   Stacks : TGLint);
  323. procedure glutSolidCone(
  324.   Base : TglDouble;  Height : TglDouble;
  325.   Slices : TGLint;  Stacks : TGLint);
  326. //jhadd
  327. procedure glutWireCylinder(
  328.   Radius : TglDouble;
  329.   Height : TglDouble;
  330.   Slices : TGLint;
  331.   Stacks : TGLint);
  332. procedure glutSolidCylinder(
  333.   Radius : TglDouble;  Height : TglDouble;
  334.   Slices : TGLint;  Stacks : TGLint);
  335.  
  336. procedure glutWireTube(
  337.   Radius : TglDouble;
  338.   Height : TglDouble;
  339.   Slices : TGLint;
  340.   Stacks : TGLint);
  341. procedure glutSolidTube(
  342.   Radius : TglDouble;  Height : TglDouble;
  343.   Slices : TGLint;  Stacks : TGLint);
  344.  
  345. procedure glutWireTorus(
  346.   innerRadius : TglDouble;  outerRadius :TglDouble;
  347.   nsides : TGLint;
  348.   rings : TGLint);
  349. procedure glutSolidTorus(
  350.   innerRadius : TglDouble;  outerRadius :TglDouble;
  351.   nsides : TGLint;
  352.   rings : TGLint);
  353. procedure glutWireDodecahedron;
  354. procedure glutSolidDodecahedron;procedure Octaheadron(ShadeType : TGLenum);procedure glutWireOctaheadron;procedure glutSolidOctaheadron;procedure Icosahedron(ShadeType : TGLenum);procedure glutWireIcosahedron;procedure glutSolidIcosahedron;procedure Tetrahedron(ShadeType : TGLenum);procedure glutWireTetrahedron;procedure glutSolidTetrahedron;{ Teapot stuff }procedure Teapot(Grid : TGLint; Scale : TglDouble; ShadeType : TGLenum);procedure glutWireTeapot(Scale : TglDouble);procedure glutSolidTeapot(Scale : TglDouble);
  355. { Generally useful stuff }
  356. procedure Diff3(a0, a1, a2, b0, b1, b2 : TGLFloat; var c : array of TGLFloat);
  357. procedure CrossProd(v1, v2 : array of TGLFloat; var prod : array of TGLFloat);procedure Normalize(var v : array of TGLFloat);
  358. var  quadObj : pGLUquadricObj=nil;  dodec : array[0..19, 0..2] of TGLFloat;
  359.  
  360. implementationprocedure DrawBox(Size : TGLFloat; DrawType : TGLenum);var  V : array[0..7, 0..2] of TGLFloat;
  361.   I : TGLint;  HalfSize : TGLFloat;
  362. begin { DrawBox }
  363.   HalfSize := Size / 2;
  364.   V[0, 0] := -HalfSize;
  365.   V[1, 0] := -HalfSize;
  366.   V[2, 0] := -HalfSize;  V[3, 0] := -HalfSize;  V[4, 0] := HalfSize;  V[5, 0] := HalfSize;  V[6, 0] := HalfSize;  V[7, 0] := HalfSize;  V[0, 1] := -HalfSize;  V[1, 1] := -HalfSize;  V[4, 1] := -HalfSize;  V[5, 1] := -HalfSize;  V[2, 1] := HalfSize;  V[3, 1] := HalfSize;  V[6, 1] := HalfSize;  V[7, 1] := HalfSize;  V[0, 2] := -HalfSize;  V[3, 2] := -HalfSize;  V[4, 2] := -HalfSize;  V[7, 2] := -HalfSize;  V[1, 2] := HalfSize;  V[2, 2] := HalfSize;  V[5, 2] := HalfSize;  V[6, 2] := HalfSize;  for I := 0 to 5 do  begin    glBegin(DrawType);      glNormal3fv(@BoxPoints[I, 0]);      glVertex3fv(@V[BoxFaces[I, 0], 0]);      glVertex3fv(@V[BoxFaces[I, 1], 0]);      glVertex3fv(@V[BoxFaces[I, 2], 0]);      glVertex3fv(@V[BoxFaces[I, 3], 0]);    glEnd;  end;end; { DrawBox }procedure glutWireSphere(  Radius : TglDouble;  Slices : TGLint;  Stacks : TGLint);begin { glutWireSphere }  if quadObj = nil then    quadObj := gluNewQuadric;  gluQuadricDrawStyle(quadObj, GLU_LINE);  gluQuadricNormals(quadObj, GLU_SMOOTH);  gluSphere(quadObj, Radius, Slices, Stacks);  gluDeleteQuadric(quadObj);  QuadObj:=nil;end; { glutWireSphere }procedure glutSolidSphere(  Radius : TglDouble;  Slices : TGLint;  Stacks : TGLint);begin { glutSolidSphere }  if quadObj = nil then    quadObj := gluNewQuadric;  gluQuadricDrawStyle(quadObj, GLU_FILL);  gluQuadricNormals(quadObj, GLU_SMOOTH);  gluSphere(quadObj, Radius, Slices, Stacks);  gluDeleteQuadric(quadObj);  QuadObj:=nil;end; { glutSolidSphere }procedure glutWireCube(Size : TglDouble);begin { glutWireCube }  DrawBox(Size, GL_LINE_LOOP);end; { glutWireCube }procedure glutSolidCube(Size : TglDouble);begin { glutSolidCube }  DrawBox(Size, GL_QUADS);end; { glutSolidCube }procedure glutWireCone(  Base : TglDouble;  Height : TglDouble;  Slices : TGLint;  Stacks : TGLint);begin { glutWireCone }  if quadObj = nil then    quadObj := gluNewQuadric;  gluQuadricDrawStyle(quadObj, GLU_LINE);  gluQuadricNormals(quadObj, GLU_SMOOTH);  gluCylinder(quadObj, base, 0.0, height, slices, stacks);  gluDeleteQuadric(quadObj);  QuadObj:=nil;end; { glutWireCone }procedure glutSolidCone(  Base : TglDouble;  Height : TglDouble;  Slices : TGLint;  Stacks : TGLint);begin { glutSolidCone }  if quadObj = nil then    quadObj := gluNewQuadric;  gluQuadricDrawStyle(quadObj, GLU_FILL);  gluQuadricNormals(quadObj, GLU_SMOOTH);  gluCylinder(quadObj, base, 0.0, height, slices, stacks);  gluDeleteQuadric(quadObj);  QuadObj:=nil;end; { glutSolidCone } //jhaddprocedure glutWireCylinder(  Radius : TglDouble;  Height : TglDouble;  Slices : TGLint;  Stacks : TGLint);begin { glutWireCylinder }  if quadObj = nil then    quadObj := gluNewQuadric;  gluQuadricDrawStyle(quadObj, GLU_LINE);  gluQuadricNormals(quadObj, GLU_SMOOTH);  glPushMatrix;  glTranslatef(0.0,0.0,height/2);  gluDisk(    quadObj, 0.0, Radius, slices,stacks);  glPopMatrix;  glPushMatrix;  glTranslatef(0.0,0.0,-height/2);  gluDisk(    quadObj, 0.0, Radius,slices,stacks);  gluCylinder(quadObj, Radius, Radius, height, slices, stacks);  glPopMatrix;  gluDeleteQuadric(quadObj);  QuadObj:=nil;end; { glutWireCylinder }procedure glutSolidCylinder(  Radius : TglDouble;  Height : TglDouble;  Slices : TGLint;  Stacks : TGLint);begin { glutSolidCylinder }  if quadObj = nil then    quadObj := gluNewQuadric;  gluQuadricDrawStyle(quadObj, GLU_FILL);  gluQuadricNormals(quadObj, GLU_SMOOTH);  glPushMatrix;  glTranslatef(0.0,0.0,height/2);  gluDisk(    quadObj, 0.0, Radius, slices,stacks);  glPopMatrix;  glPushMatrix;  glTranslatef(0.0,0.0,-height/2);  gluDisk(    quadObj, 0.0, Radius,slices,stacks);  gluCylinder(quadObj, Radius, Radius, height, slices, stacks);  glPopMatrix;  gluDeleteQuadric(quadObj);  QuadObj:=nil;end; { glutSolidCylinder }procedure glutWireTube(  Radius : TglDouble;  Height : TglDouble;  Slices : TGLint;  Stacks : TGLint);begin { glutWireTube }  if quadObj = nil then    quadObj := gluNewQuadric;  gluQuadricDrawStyle(quadObj, GLU_LINE);  gluQuadricNormals(quadObj, GLU_SMOOTH);  glPushMatrix;  glTranslatef(0.0,0.0,-height/2);  gluCylinder(quadObj, Radius, Radius, height, slices, stacks);  glPopMatrix;  gluDeleteQuadric(quadObj);  QuadObj:=nil;end; { glutWireTube }procedure glutSolidTube(  Radius : TglDouble;  Height : TglDouble;  Slices : TGLint;  Stacks : TGLint);begin { glutSolidTube }  if quadObj = nil then    quadObj := gluNewQuadric;  gluQuadricDrawStyle(quadObj, GLU_FILL);  gluQuadricNormals(quadObj, GLU_SMOOTH);  glPushMatrix;  glTranslatef(0.0,0.0,-height/2);  gluCylinder(quadObj, Radius, Radius, height, slices, stacks);  glPopMatrix;  gluDeleteQuadric(quadObj);  QuadObj:=nil;end; { glutSolidTube }procedure Doughnut(  inr : TGLFloat;  outR : TGLFloat;  nsides : TGLint;  rings : TGLint;  DrawType :TGLenum);var  i, j : integer;  theta, phi, theta1, phi1 : TGLFloat;  p0, p1, p2, p3,  n0, n1, n2, n3 : array[0..2] of TGLFloat;begin { Doughnut }  for i := 0 to rings - 1 do  begin    theta := i *2.0 * PI / rings;    theta1 := (i + 1) * 2.0 * PI / rings;    for j := 0 to nsides - 1 do    begin      phi := j *2.0 * PI / nsides;      phi1 := (j + 1) * 2.0 * PI / nsides;
  367.         p0[0] := cos(theta) * (outR + inr * cos(phi));
  368.       p0[1] := -sin(theta) * (outR + inr * cos(phi));      p0[2] := inr * sin(phi);
  369.         p1[0] := cos(theta1) * (outR + inr * cos(phi));
  370.       p1[1] := -sin(theta1) * (outR + inr * cos(phi));      p1[2] := inr * sin(phi);
  371.         p2[0] := cos(theta1) * (outR + inr * cos(phi1));
  372.       p2[1] := -sin(theta1) * (outR + inr * cos(phi1));      p2[2] := inr * sin(phi1);
  373.       p3[0] := cos(theta) * (outR + inr * cos(phi1));      p3[1] := -sin(theta) * (outR + inr * cos(phi1));      p3[2] := inr * sin(phi1);      n0[0] := cos(theta) * (cos(phi));      n0[1] := -sin(theta) * (cos(phi));      n0[2] := sin(phi);      n1[0] := cos(theta1) * (cos(phi));      n1[1] := -sin(theta1) * (cos(phi));      n1[2] := sin(phi);      n2[0] := cos(theta1) * (cos(phi1));      n2[1] := -sin(theta1) * (cos(phi1));      n2[2] := sin(phi1);      n3[0] := cos(theta) * (cos(phi1));      n3[1] := -sin(theta) * (cos(phi1));      n3[2] := sin(phi1);      glBegin(DrawType);      glNormal3fv(@n3[0]);      glVertex3fv(@p3[0]);      glNormal3fv(@n2[0]);      glVertex3fv(@p2[0]);      glNormal3fv(@n1[0]);      glVertex3fv(@p1[0]);      glNormal3fv(@n0[0]);      glVertex3fv(@p0[0]);      glEnd();    end;  end;end; { Doughnut }procedure glutWireTorus(  innerRadius : TglDouble;  outerRadius :TglDouble;  nsides : TGLint;  rings : TGLint);begin { glutWireTorus }  Doughnut(innerRadius, outerRadius, nsides, rings, GL_LINE_LOOP);end; { glutWireTorus }procedure glutSolidTorus(  innerRadius : TglDouble;  outerRadius :TglDouble;  nsides : TGLint;  rings : TGLint);begin { glutSolidTorus }  Doughnut(innerRadius, outerRadius, nsides, rings, GL_QUADS);end; { glutSolidTorus }procedure initDodecahedron;var
  374.   alpha, beta : TGLFloat;
  375. begin { initDodecahedron }
  376.   alpha := sqrt(2.0 / (3.0 + sqrt(5.0)));
  377.   beta := 1.0 + sqrt(6.0 / (3.0 + sqrt(5.0)) -
  378.     2.0 + 2.0 * sqrt(2.0 / (3.0 + sqrt(5.0))));
  379.  
  380.   dodec[0, 0] := -alpha;  dodec[0, 1] := 0;       dodec[0, 2] := beta;
  381.   dodec[1, 0] := alpha;   dodec[1, 1] := 0;       dodec[1, 2] := beta;
  382.   dodec[2, 0] := -1;      dodec[2, 1] := -1;      dodec[2, 2] := -1;
  383.   dodec[3, 0] := -1;      dodec[3, 1] := -1;      dodec[3, 2] := 1;
  384.   dodec[4, 0] := -1;      dodec[4, 1] := 1;       dodec[4, 2] := -1;
  385.   dodec[5, 0] := -1;      dodec[5, 1] := 1;       dodec[5, 2] := 1;
  386.   dodec[6, 0] := 1;       dodec[6, 1] := -1;      dodec[6, 2] := -1;
  387.   dodec[7, 0] := 1;       dodec[7, 1] := -1;      dodec[7, 2] := 1;
  388.   dodec[8, 0] := 1;       dodec[8, 1] := 1;       dodec[8, 2] := -1;
  389.   dodec[9, 0] := 1;       dodec[9, 1] := 1;       dodec[9, 2] := 1;
  390.   dodec[10, 0] := beta;   dodec[10, 1] := alpha;  dodec[10, 2] := 0;
  391.   dodec[11, 0] := beta;   dodec[11, 1] := -alpha; dodec[11, 2] := 0;
  392.   dodec[12, 0] := -beta;  dodec[12, 1] := alpha;  dodec[12, 2] := 0;
  393.   dodec[13, 0] := -beta;  dodec[13, 1] := -alpha; dodec[13, 2] := 0;
  394.   dodec[14, 0] := -alpha; dodec[14, 1] := 0;      dodec[14, 2] := -beta;
  395.   dodec[15, 0] := alpha;  dodec[15, 1] := 0;      dodec[15, 2] := -beta;
  396.   dodec[16, 0] := 0;      dodec[16, 1] := beta;   dodec[16, 2] := alpha;
  397.   dodec[17, 0] := 0;      dodec[17, 1] := beta;   dodec[17, 2] := -alpha;
  398.   dodec[18, 0] := 0;      dodec[18, 1] := -beta;  dodec[18, 2] := alpha;
  399.   dodec[19, 0] := 0;      dodec[19, 1] := -beta;  dodec[19, 2] := -alpha;
  400. end; { initDodecaheadron }procedure Diff3(  a0, a1, a2, b0, b1, b2 : TGLFloat;  var c : array of TGLFloat);begin { Diff3 }  c[0] := a0 - b0;  c[1] := a1 - b1;  c[2] := a2 - b2;end; { Diff3 }procedure CrossProd(  v1, v2 : array of TGLFloat;  var prod : array of TGLFloat);var  p : array[0..2] of TGLFloat;begin { CrossProd }  p[0] := v1[1] * v2[2] - v2[1] * v1[2];  p[1] := v1[2] * v2[0] - v2[2] * v1[0];
  401.   p[2] := v1[0] * v2[1] - v2[0] * v1[1];
  402.   prod[0] := p[0];
  403.   prod[1] := p[1];
  404.   prod[2] := p[2];
  405. end; { CrossProd }procedure Normalize(var v : array of TGLFloat);var  d : TGLFloat;begin { Normalize }  d := sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);  d := 1 / d;
  406.   v[0] := v[0] * d;
  407.   v[1] := v[1] * d;
  408.   v[2] := v[2] * d;
  409. end; { Normalize }procedure Pentagon(a, b, c, d, e : integer; shadeType : TGLenum);var  n0, d1, d2 : array[0..2] of TGLFloat;begin { Pentagon }
  410.   Diff3(dodec[a, 0], dodec[a, 1], dodec[1, 2],
  411.         dodec[b, 0], dodec[b, 1], dodec[b, 2], d1);
  412.   Diff3(dodec[b, 0], dodec[b, 1], dodec[b, 2],
  413.         dodec[c, 0], dodec[c, 1], dodec[c, 2], d2);
  414.   CrossProd(d1, d2, n0);
  415.   Normalize(n0);
  416.  
  417.   glBegin(shadeType);
  418.     glNormal3fv(@n0[0]);
  419.     glVertex3fv(@dodec[a, 0]);
  420.     glVertex3fv(@dodec[b, 0]);
  421.     glVertex3fv(@dodec[c, 0]);
  422.     glVertex3fv(@dodec[d, 0]);
  423.     glVertex3fv(@dodec[e, 0]);
  424.   glEnd();
  425. end; { Pentagon }
  426.  
  427. procedure Dodecahedron(DrawType : TGLenum);begin { Dodecahedron }
  428.   pentagon(0, 1, 9, 16, 5, DrawType);
  429.   pentagon(1, 0, 3, 18, 7, DrawType);
  430.   pentagon(1, 7, 11, 10, 9, DrawType);
  431.   pentagon(11, 7, 18, 19, 6, DrawType);
  432.   pentagon(8, 17, 16, 9, 10, DrawType);
  433.   pentagon(2, 14, 15, 6, 19, DrawType);
  434.   pentagon(2, 13, 12, 4, 14, DrawType);
  435.   pentagon(2, 19, 18, 3, 13, DrawType);
  436.   pentagon(3, 0, 5, 12, 13, DrawType);
  437.   pentagon(6, 15, 8, 10, 11, DrawType);
  438.   pentagon(4, 17, 8, 15, 14, DrawType);
  439.   pentagon(4, 12, 5, 16, 17, DrawType);
  440. end; { Dodecahedron }
  441.  
  442. procedure glutWireDodecahedron;begin { glutWireDodecahedron }
  443.   Dodecahedron(GL_LINE_LOOP);
  444. end; { glutWireDodecahedron }
  445.  
  446. procedure glutSolidDodecahedron;
  447. begin { glutSolidDodecahedron }
  448.   Dodecahedron(GL_TRIANGLE_FAN);
  449. end; { glutSolidDodecahedron }
  450.  
  451. procedure RecordItem(n1, n2, n3 :  array of TGLFloat; ShadeType : TGLenum);var  q0, q1 : array[0..2] of TGLFloat;begin { RecordItem }  Diff3(n1[0], n1[1], n1[2], n2[0], n2[1], n2[2], q0);
  452.   Diff3(n2[0], n2[1], n2[2], n3[0], n3[1], n3[2], q1);
  453.   CrossProd(q0, q1, q1);
  454.   Normalize(q1);
  455.  
  456.   glBegin(shadeType);
  457.     glNormal3fv(@q1[0]);
  458.     glVertex3fv(@n1[0]);
  459.     glVertex3fv(@n2[0]);
  460.     glVertex3fv(@n3[0]);
  461.   glEnd;
  462. end; { RecordItem }
  463. procedure SubDivide(  v0, v1, v2 : array of TGLFloat;
  464.   ShadeType : TGLenum);
  465. var
  466.   Depth : Integer;
  467.   w0, w1, w2 : array[0..2] of TGLFloat;
  468.   l : TGLFloat;
  469.   i, j, k, n : Integer;
  470. begin { SubDivide }
  471.   Depth := 1;
  472.   for i := 0 to Depth - 1 do
  473.   begin
  474.     j := 0;
  475.     while i + j < Depth do
  476.     begin
  477.       k := depth - i - j;
  478.       for n := 0 to 2 do
  479.       begin
  480.         w0[n] := (i * v0[n] + j * v1[n] + k * v2[n]) / depth;
  481.         w1[n] := ((i + 1) * v0[n] + j * v1[n] + (k - 1) * v2[n])
  482.           / depth;
  483.         w2[n] := (i * v0[n] + (j + 1) * v1[n] + (k - 1) * v2[n])
  484.           / depth;
  485.       end;
  486.       l := sqrt(w0[0] * w0[0] + w0[1] * w0[1] + w0[2] * w0[2]);
  487.       w0[0] := w0[0] / l;
  488.       w0[1] := w0[1] / l;
  489.       w0[2] := w0[2] / l;
  490.       l := sqrt(w1[0] * w1[0] + w1[1] * w1[1] + w1[2] * w1[2]);
  491.       w1[0] := w1[0] / l;
  492.       w1[1] := w1[1] / l;
  493.       w1[2] := w1[2] / l;
  494.       l := sqrt(w2[0] * w2[0] + w2[1] * w2[1] + w2[2] * w2[2]);
  495.       w2[0] := w2[0] / l;
  496.       w2[1] := w2[1] / l;
  497.       w2[2] := w2[2] / l;
  498.       RecordItem(w1, w0, w2, ShadeType);
  499.       Inc(j);
  500.     end;
  501.   end;
  502. end; { SubDivide }
  503.  
  504. procedure DrawTriangle(
  505.   I : Integer;
  506.   Data : Array of TGLfloat3v;
  507.   NDX :  Array of TInteger3v;
  508.   ShadeType : TGLenum);
  509. var
  510.   X0, X1, X2 : TGLfloat3v;
  511. begin { DrawTriangle }
  512.   X0 := Data[NDX[i, 0]];
  513.   X1 := Data[NDX[i, 1]];
  514.   X2 := Data[NDX[i, 2]];
  515.   SubDivide(X0, X1, X2, ShadeType);
  516. end; { DrawTriangle }
  517.  
  518. procedure Octaheadron(ShadeType : TGLenum);
  519. var
  520.   I : Integer;
  521. begin { Octaheadron }
  522.   for I := 0 to 7 do
  523.     DrawTriangle(I, OctData, OctIndex, ShadeType);
  524. end; { Octaheadron }
  525.  
  526. procedure glutWireOctaheadron;
  527. begin { glutWireOctaheadron }
  528.   Octaheadron(GL_LINE_LOOP);
  529. end; { glutWireOctaheadron }
  530.  
  531. procedure glutSolidOctaheadron;begin { glutSolidOctaheadron }
  532.   Octaheadron(GL_TRIANGLES);
  533. end; { glutSolidOctaheadron }
  534.  
  535. procedure Icosahedron(ShadeType : TGLenum);
  536. var
  537.   I : Integer;
  538. begin { Icosahedron }
  539.   for I := 0 to 19 do
  540.     DrawTriangle(I, IcoData, IcoIndex, ShadeType);
  541. end; { Icosahedron }
  542.  
  543. procedure glutWireIcosahedron;begin { glutWireIcosahedron }  Icosahedron(GL_LINE_LOOP);end; { glutWireIcosahedron }procedure glutSolidIcosahedron;begin { glutSolidIcosahedron }
  544.   Icosahedron(GL_TRIANGLES);
  545. end; { glutSolidIcosahedron }
  546.  
  547. procedure Tetrahedron(ShadeType : TGLenum);
  548. var
  549.   I : Integer;
  550. begin { Tetrahedron }
  551.   for I := 0 to 3 do
  552.     DrawTriangle(I, TetData, TetIndex, ShadeType);
  553. end; { Tetrahedron }
  554.  
  555. procedure glutWireTetrahedron;begin { glutWireTetrahedron }  Tetrahedron(GL_LINE_LOOP);end; { glutWireTetrahedron }
  556.  
  557. procedure glutSolidTetrahedron;begin { glutSolidTetrahedron }  Tetrahedron(GL_TRIANGLES);
  558. end; { glutSolidTetrahedron }
  559.  
  560. { Teapot stuff }procedure Teapot(Grid : TGLint; Scale : TglDouble; ShadeType : TGLenum);var  P, Q, R, S : Array[0..3, 0..3, 0..2] of TGLFloat;  I, J, K, L : TGLint;begin { Teapot }
  561.   glPushAttrib(GL_ENABLE_BIT or GL_EVAL_BIT);
  562.   glEnable(GL_AUTO_NORMAL);
  563.   glEnable(GL_NORMALIZE);
  564.   glEnable(GL_MAP2_VERTEX_3);
  565.   glEnable(GL_MAP2_TEXTURE_COORD_2);
  566.   glPushMatrix;
  567.   glRotatef(270.0, 1.0, 0.0, 0.0);
  568.   glScalef(0.5 * Scale, 0.5 * Scale, 0.5 * Scale);
  569.   glTranslatef(0.0, 0.0, -1.5);
  570.   for I := 0 to 9 do
  571.   begin
  572.     for J := 0 to 3 do
  573.       for K := 0 to 3 do
  574.         for L := 0 to 2 do
  575.         begin
  576.           P[J, K, L] := TeaData[PatchData[I, J * 4 + K], L];
  577.           Q[J, K, L] := TeaData[PatchData[I, J * 4 + (3 - K)], L];
  578.           if L = 1 then
  579.             Q[J, K, L] := -Q[J, K, L];
  580.           if i < 6 then
  581.           begin
  582.             R[J, K, L] := TeaData[PatchData[I, J * 4 + (3 - K)], L];
  583.             if L = 0 then
  584.               R[J, K, L] := -R[J, K, L];
  585.             S[J, K, L] := TeaData[PatchData[I, J * 4 + K], L];
  586.             if L = 0 then
  587.               S[J, K, L] := -S[J, K, L];
  588.             if L = 1 then
  589.               S[J, K, L] := -S[J, K, L];
  590.           end;
  591.         end;
  592.     glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, @TeaTex[0,0,0]);
  593.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, @P[0, 0, 0]);
  594.     glMapGrid2f(Grid, 0.0, 1.0, Grid, 0.0, 1.0);
  595.     glEvalMesh2(ShadeType, 0, Grid, 0, Grid);
  596.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, @Q[0, 0, 0]);
  597.     glEvalMesh2(ShadeType, 0, Grid, 0, Grid);
  598.     if I < 6 then
  599.     begin
  600.       glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, @R[0, 0, 0]);
  601.       glEvalMesh2(ShadeType, 0, Grid, 0, Grid);
  602.       glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, @S[0, 0, 0]);
  603.       glEvalMesh2(ShadeType, 0, Grid, 0, Grid);
  604.     end;
  605.   end;
  606.   glPopMatrix;
  607.   glPopAttrib;
  608. end; { Teapot }
  609.  
  610. procedure glutWireTeapot(Scale : TglDouble);begin { glutWireTeapot }
  611.   Teapot(10, Scale, GL_LINE);
  612. end; { glutWireTeapot }
  613. procedure glutSolidTeapot(Scale : TglDouble);
  614. begin { glutSolidTeapot }
  615.   Teapot(14, Scale, GL_FILL);
  616. end; { glutSolidTeapot }
  617.  
  618. initialization
  619.   initDodecahedron;
  620.  
  621. end.